package com.boboPlanet.service.impl;

import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.result.ResponseResult;
import com.boboPlanet.dao.DictConfigMapper;
import com.boboPlanet.dao.DictConfigMapperConvert;
import com.boboPlanet.entity.dto.DictConfigDTO;
import com.boboPlanet.entity.po.DictConfigPO;
import com.boboPlanet.entity.vo.DictConfigVO;
import com.boboPlanet.entity.vo.InvalidConfigVO;
import com.boboPlanet.service.DictConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/9/19
 **/
@Service
@Slf4j
public class DictConfigServiceImpl implements DictConfigService {

    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();


    @Autowired
    private DictConfigMapper dictConfigMapper;

    @Override
    public ResponseResult<DictConfigVO> findDictConfigByPage(Integer page, Integer size, String configName) {
        //当前mysql版本较低，分页不支持
        page = (1-page)*size;
        List<DictConfigPO> dictConfigList = dictConfigMapper.findDictConfigByPage(page,size,configName);
        List<DictConfigDTO> dictConfigDTOList = new ArrayList<>(dictConfigList.size());
        List<DictConfigDTO> dtos = DictConfigMapperConvert.INSTANCE.poList2dtoList(dictConfigList);
        DictConfigVO configVO = new DictConfigVO(dtos, ObjectUtils.isEmpty(dtos) || dtos.size()==0?0:dtos.get(0).getTotals());
//        dictConfigList.forEach(val ->{
//            //BeanUtils.copyProperties(val,dictConfigVO);
//            DictConfigDTO dto = DictConfigMapperConvert.INSTANCE.po2dto(val);
//            dictConfigDTOList.add(dto);
//        });
        return ResponseResult.isSuccess(configVO);
    }

    @Override
    public ResponseResult<List<InvalidConfigVO>> getInvalidFlagData(String configName) {
        Lock readLock = readWriteLock.readLock();
        List<DictConfigPO> configPOS = dictConfigMapper.getInvalidFlagData(configName);
        List<InvalidConfigVO> invalidConfigVOS = DictConfigMapperConvert.INSTANCE.poList2voList(configPOS);
        return ResponseResult.isSuccess(invalidConfigVOS);
    }

    @Override
    public Boolean openInvalidConfig(Long id) {
        Lock writeLock = readWriteLock.writeLock();
        try {
            writeLock.lock();
//            boolean tryLock = writeLock.tryLock();
//            if (!tryLock){
//                throw BusinessException.build(ResultEnum.SYS_BUSY_AFTER_TRY.getMsg());
//            }
            if (ObjectUtils.isEmpty(id)){
                throw BusinessException.build("开启配置参数【id】不能为空");
            }
            return dictConfigMapper.openInvalidConfig(id);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }finally {
            writeLock.unlock();
        }
        return false;
    }

    @Override
    public Boolean invalidConfig(Long id) {
        Lock writeLock = readWriteLock.writeLock();
        try {
            writeLock.lock();
            if (ObjectUtils.isEmpty(id)){
                throw BusinessException.build("作废配置参数【id】不能为空");
            }
            return dictConfigMapper.invalidConfig(id);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }finally {
            writeLock.unlock();
        }
        return false;
    }
}
